$(function(){
  var mobile = $('#mobile'),
  captcha = $('#captcha'),
  remark = $('#remark'),
  username = $('#username'),
  captchaImg = $('#captchaImg'),
  sessionId;
  var validate = {
    _username: false,
    _msgsend: false,
    _mobile: false,
    _captcha: false,
    checkUname: function (uanme) {
      if (!required(uanme)) {
        $.sendWarning('请填写联系人姓名', 2000);
        validate._username = false;
        return;
      };
      validate._username = true;
    },
    //校验手机号码
    checkmobile: function (mobile, callback) {
      if (!required(mobile)) {
        $.sendWarning('请填写手机号', 2000);
        validate._mobile = false;
        return;
      }
      if (mobile.length != 11) {
        // showMsg.mobile(2, "手机号码长度不正确。");
        $.sendWarning('手机号码长度不正确。', 2000);
        validate._mobile = false;
        return;
      };
      if (!isPhone(mobile)) {
        $.sendWarning('手机号码格式不正确，请重新输入！', 2000);
        validate._mobile = false;
        return;
      };
      // showMsg.mobile(1);
      validate._mobile = true;
    },
    checkCode: function (code) {
      if (!required(code)) {
        $.sendWarning('请填写验证码！', 2000);
        validate._captcha = false;
        return;
      }
      if (code.length != 4) {
        $.sendWarning('验证码长度不正确！', 2000);
        validate._captcha = false;
        return;
      }
      validate._captcha = true;
    }, sub: function () {
      Http({
        type: 'POST',
        url: '/refer/saveReferInfo',
        contentType: "application/json;charset=UTF-8",
        dataType: 'json',
        data: JSON.stringify({
          referName: username.val(),
          referPhone: mobile.val(),
          code: captcha.val(),
          sessionId: sessionId
        })
      }, function (data) {
        if (data.code != '0000') {
          $.sendWarning(data.msg, 2000);
        } else {
          $.sendWarning('您的建站需求已提交成功，我们会尽快与您联系', 2000);
          validate.codeSrc();
        }
      })
    }, codeSrc: function () {
      $.ajax({
        type: "GET",
        url: path + "/refer/getAuthCode",
        success: function (data, status, req) {
          captchaImg.attr('src', 'data:image/png;base64,' + data.data.imgStr);
          sessionId = data.data.sessionId
        }
      });
    }
  }
  username.on('keyup', function () {
    $(this).val().length == 0 ? validate._username = false : validate._username = true
  })
  mobile.on('blur', function () {
    $(this).val().length == 0 ? validate._mobile = false : validate.checkmobile(mobile.val())
  })
  captcha.on('blur', function () {
    $(this).val().length == 0 ? validate._captcha = false : validate.checkCode(captcha.val())
  })
  $('#sub').on('click', function (e) {
    e.preventDefault();
    if (!validate._username) {
      validate.checkUname(username.val());
      return;
    }
    if (!validate._mobile) {
      validate.checkmobile(mobile.val());
      return;
    }
    if (!validate._captcha) {
      validate.checkCode(captcha.val());
      return;
    }
    validate.sub();
  })
  yfx.iptActive('.ipt', '.icon', 'icon_active')
  validate.codeSrc();
  
  var href = window.location.href
  var search = yfx.getQuery('path')||'index';
  if (search) {
    $('.' + search).parent().siblings().find('a').removeClass('nav_active');
    $('.' + search).addClass('nav_active');
  }
})